local skel = fk.CreateSkill {
  name = "lb__shiyue",
  max_branches_use_time = {
    ["damage"] = {
      [Player.HistoryTurn] = 1
    },
    ["damaged"] = {
      [Player.HistoryTurn] = 1
    },
  },
}

Fk:loadTranslationTable {
  ["lb__shiyue"] = "诗约",
  [":lb__shiyue"] = "每回合各限一次，当你造成或受到伤害后，你可弃置一张牌以令一名角色获得<a href=':lb__zhuhuo'><font color='red'>〖逐火〗</font></a>，若其已拥有，改为令其选择一个未拥有的此牌点数对应<a href='#lb__shiyue_zhuhuozhe'><font color='red'>“逐火者”</font></a>的技能获得之直到其下回合结束。",

  ["#lb__shiyue_choose_nil"] = "诗约：令一名角色获得〖逐火〗",
  ["#lb__shiyue_choose"] = "诗约：令一名角色获得〖逐火〗，若其已拥有，改为令其从%arg的%arg2中未拥有的技能选择一个获得直到其下回合结束",
  ["#lb__shiyue_choose_skill"] = "诗约：请选择一个技能获得直到你下回合结束",

  ["@[lb__shiyue]lb__shiyue"] = "诗约",
  ["@[skills]lb__shiyue-round"] = "如初",

  ["#@[lb__shiyue]lb__shiyue_tip"] = "诗约（灰色技能于当前回合结束后失去）",

  ["#lb__shiyue_zhuhuozhe"] =
      "#\"<b>逐火者</b>\"<br>" ..
      "A：阿格莱雅：<a href=':lb__zhiming'><font color='red'>〖织命〗</font></a> <a href=':lb__fengci'><font color='red'>〖蜂刺〗</font></a><br>" ..
      "2：缇宝：<a href=':lb__jingsuo'><font color='red'>〖径梭〗</font></a> <a href=':lb__zhijianr'><font color='red'>〖致笺〗</font></a><br>" ..
      "3：那刻夏：<a href=':lb__qiongli'><font color='red'>〖穷理〗</font></a> <a href=':lb__lianjin'><font color='red'>〖炼金〗</font></a><br>" ..
      "4：风堇：<a href=':lb__fengxi'><font color='red'>〖风息〗</font></a> <a href=':lb__hongze'><font color='red'>〖虹泽〗</font></a><br>" ..
      "5：万敌：<a href=':lb__fayan'><font color='red'>〖伐焱〗</font></a><br>" ..
      "6：赛飞儿：<a href=':lb__shenjie'><font color='red'>〖神捷〗</font></a> <a href=':lb__tianqi'><font color='red'>〖天欺〗</font></a><br>" ..
      "7：遐蝶：<a href=':lb__youfu'><font color='red'>〖幽抚〗</font></a><br>" ..
      "8：白厄：<a href=':lb__lizhou'><font color='red'>〖黎昼〗</font></a> <a href=':lb__fushi'><font color='red'>〖负世〗</font></a><br>" ..
      "9：海瑟音：<a href=':lb__huqiu'><font color='red'>〖互囚〗</font></a> <a href=':lb__huli'><font color='red'>〖互礼〗</font></a><br>" ..
      "10：长夜月：<a href=':lb__mengyin'><font color='red'>〖梦洇〗</font></a> <a href=':lb__kouji'><font color='red'>〖叩寂〗</font></a><br>" ..
      "J：丹恒腾荒：<a href=':lb__bishi'><font color='red'>〖庇世〗</font></a> <a href=':lb__tongzhou'><font color='red'>〖同舟〗</font></a><br>" ..
      "Q：刻律德菈：<a href=':lb__shengbian'><font color='red'>〖擢英〗</font></a><br>" ..
      "K：记忆开拓者：<a href=':lb__shuyi'><font color='red'>〖书忆〗</font></a><br>",

  ["$lb__shiyue1"] = "愿「浪漫」，永不离席。",
  ["$lb__shiyue2"] = "愿「门径」，再无别离。 ",
  ["$lb__shiyue3"] = "愿「理性」，启蒙真理。",
  ["$lb__shiyue4"] = "愿「天空」，治愈晨昏。 ",
  ["$lb__shiyue5"] = "愿「纷争」，加冕成王。",
  ["$lb__shiyue6"] = "愿「诡计」，皆为游戏。",
  ["$lb__shiyue7"] = "愿「生死」，萌发新蕊。",
  ["$lb__shiyue8"] = "愿「负世」，照拂黎明。",
  ["$lb__shiyue9"] = "愿「海洋」，奏响欢宴。",
  ["$lb__shiyue10"] = "愿「岁月」，守望长夜。",
  ["$lb__shiyue11"] = "愿「大地」，捍卫前路。",
  ["$lb__shiyue12"] = "愿「律法」，遍入群星。",
  ["$lb__shiyue13"] = "愿「开拓」，共赴「爱」的约定。",
  
  ["$lb__shiyue14"] = "群星啊，请为英雄闪耀！",
  ["$lb__shiyue15"] = "百花啊，请为明天绽放！",
  ["$lb__shiyue16"] = "愿「世界」，如你我所愿。",

}

Fk:addQmlMark {
  name = "lb__shiyue",
  how_to_show = function(name, value)
    if type(value) == "table" then
      local num = 0
      for _, val in pairs(value) do
        if val then
          num = num + 1
        end
      end
      if num > 0 then
        return tostring(num)
      end
    end
    return " "
  end,
  qml_data = function(name, value, player)
    local change = {}
    local black = player:getTableMark("lb__shiyue-turn")
    for id, skills in pairs(value) do
      if skills then
        local list = { id }
        for _, skill in ipairs(skills) do
          local enabled = black[id] and table.contains(black[id], skill)
          table.insert(list, { skill, enabled and 1 or 0 })
        end
        table.insert(change, list)
      end
    end
    return change
  end,
  qml_path = "packages/lb_kill/qml/ShiYueMarkDetailBox"
}



local shiyue_spec = {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and not player:isNude() then
      if event == fk.Damage then
        return skel:withinBranchTimesLimit(player, "damage", Player.HistoryTurn)
      else
        return skel:withinBranchTimesLimit(player, "damaged", Player.HistoryTurn)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local s, ret = room:askToUseActiveSkill(player, {
      skill_name = "#lb__shiyue_dis",
      prompt = "#lb__shiyue_dis_1",
    })
    if s and ret then
      event:setCostData(self, { cards = ret.cards })
      return true
    end
  end,
  ---@param player ServerPlayer
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, event == fk.Damage and "damage" or "damaged")
    local room = player.room
    local cards = event:getCostData(self).cards
    local num = Fk:getCardById(cards[1]):getNumberStr()
    local audio = Fk:getCardById(cards[1]).number
    room:throwCard(cards, skel.name, player)
    if player.dead then return end
    local list = {
      ["A"] = { "lb__agelaiya", "lb__zhiming", "lb__fengci" },
      ["2"] = { "lb__tibao", "lb__jingsuo", "lb__zhijianr" },
      ["3"] = { "lb__nakexia", "lb__qiongli", "lb__lianjin" },
      ["4"] = { "lb__fengjin", "lb__fengxi", "lb__hongze" },
      ["5"] = { "lb__wandi", "lb__fayan" },
      ["6"] = { "lb__saifeier", "lb__shenjie", "lb__tianqi" },
      ["7"] = { "lb__xiadie", "lb__youfu" },
      ["8"] = { "lb__baie", "lb__lizhou", "lb__fushi" },
      ["9"] = { "lb__haiseyin", "lb__huqiu", "lb__huli" },
      ["10"] = { "lb__changyeyue", "lb__mengyin", "lb__kouji" },
      ["J"] = { "lb__danhengtenghuang", "lb__bishi", "lb__tongzhou" },
      ["Q"] = { "lb__kelvdela", "lb__shengbian" },
      ["K"] = { "lb__jiyikaituozhe", "lb__shuyi" }
    }
    local prompt = "#lb__shiyue_choose_nil"
    local name, skills = "", {}
    if list[num] then
      name = list[num][1]
      for i = 2, #list[num], 1 do
        table.insert(skills, list[num][i])
      end
      prompt = "#lb__shiyue_choose:::" .. name .. ":" .. table.concat(table.map(skills, function(skill)
        return "〖" .. Fk:translate(skill) .. "〗"
      end), "/")
    end
    local tos = room:askToChoosePlayers(player, {
      targets = room:getAlivePlayers(),
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      cancelable = false,
      prompt = prompt,
    })
    if #tos == 0 then return end
    room:notifySkillInvoked(player, skel.name, "support", tos)
    local to = tos[1]
    if to:hasSkill("lb__zhuhuo", true) or to:hasSkill("lb__fenxiao_zhuhuo", true) then -- 淦
      player:broadcastSkillInvoke(skel.name, audio)
      for i = #skills, 1, -1 do
        if to:hasSkill(skills[i], true) then
          table.remove(skills, i)
        end
      end
      if #skills > 0 then
        local skill = skills[1]
        if #skills > 1 then
          skill = room:askToChoice(to, {
            choices = skills,
            skill_name = skel.name,
            prompt = "#lb__shiyue_choose_skill"
          })
        end
        local player_skills = player:getTableMark("@[lb__shiyue]lb__shiyue")
        player_skills[tostring(to.id)] = player_skills[tostring(to.id)] or {}
        table.insertIfNeed(player_skills[tostring(to.id)], skill)
        room:setPlayerMark(player, "@[lb__shiyue]lb__shiyue", player_skills)
        room:addTableMarkIfNeed(player, "@[skills]lb__shiyue-round", skill)
        room:handleAddLoseSkills(to, skill)
      end
    else
      player:broadcastSkillInvoke(skel.name, 13 + math.random(3))
      room:handleAddLoseSkills(to, "lb__zhuhuo")
    end
  end,
}

skel:addEffect(fk.Damage, shiyue_spec, { check_skill_limit = true })
skel:addEffect(fk.Damaged, shiyue_spec, { check_skill_limit = true })

skel:addEffect(fk.TurnStart, {
  can_refresh = Util.TrueFunc,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local player_skills = player:getTableMark("@[lb__shiyue]lb__shiyue")
    local skills = table.clone(player_skills[tostring(target.id)] or {})
    if #skills > 0 then
      room:setPlayerMark(player, "lb__shiyue-turn", { [tostring(target.id)] = skills })
      local Turn = room.logic:getCurrentEvent()
      if Turn then
        Turn:addCleaner(function()
          for _, skill in ipairs(skills) do
            player_skills = player:getTableMark("@[lb__shiyue]lb__shiyue") --需要重新获取
            if player_skills[tostring(target.id)] and type(player_skills[tostring(target.id)]) == "table" then
              if table.removeOne(player_skills[tostring(target.id)], skill) then
                if #player_skills[tostring(target.id)] == 0 then
                  player_skills[tostring(target.id)] = nil
                end
                room:setPlayerMark(player, "@[lb__shiyue]lb__shiyue", player_skills)
                if target:hasSkill(skill, true) then
                  room:handleAddLoseSkills(target, "-" .. skill)
                end
              end
            end
          end
        end)
      end
    end
  end,
})
--[[
skel:addAcquireEffect(function(self, player, is_start) --test
  local list = { "lb__zhiming", "lb__fengci", "lb__jingsuo", "lb__zhijianr",
    "lb__qiongli", "lb__lianjin", "lb__huqiu", "lb__huli",
    "lb__shenjie", "lb__tianqi", "lb__lizhou", "lb__fushi" }
  player.room:setPlayerMark(player, "@[lb__shiyue]lb__shiyue", { [tostring(player.id)] = list })
  player.room:setPlayerMark(player, "@[skills]lb__shiyue-round", list)
end)
--]]
skel:addLoseEffect(function(self, player, is_death)
  local room = player.room
  room:setPlayerMark(player, "lb__shiyue-turn", 0)
  room:setPlayerMark(player, "@[skills]lb__shiyue-round", 0)
  local player_skills = player:getTableMark("@[lb__shiyue]lb__shiyue")
  for _, to in ipairs(room.players) do
    local skills = player_skills[tostring(to.id)] or {}
    for _, skill in ipairs(skills) do
      if to:hasSkill(skill, true) then
        room:handleAddLoseSkills(to, "-" .. skill)
      end
    end
  end
  room:setPlayerMark(player, "@[lb__shiyue]lb__shiyue", 0)
end)

return skel
